from django.shortcuts import render

# Create your views here.
from django.contrib.auth.models import Permission, ContentType, Group
from users.models import User
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet
from permission.serializers import PermissionSerialize, ContentTypeSerialzier, GroupSerialzier, UserSerialzier
from meiduo_mall.utils.page import PageNum

"""
{
        "counts": "权限总数量",
        "list": [
            {
                "id": "权限id",
                "name": "权限名称",
                "codename": "权限识别名",
                "content_type": "权限类型"
            },
            ...
        ],
        "page": "当前页码",
        "pages": "总页码",
        "pagesize": "页容量"
    }
"""


class PermissionView(ModelViewSet):
    queryset = Permission.objects.all()
    serializer_class = PermissionSerialize
    pagination_class = PageNum

    def content_types(self, request):
        # 查询全选分类
        content = ContentType.objects.all()
        # 返回结果
        ser = ContentTypeSerialzier(content, many=True)
        return Response(ser.data)


class GroupView(ModelViewSet):
    queryset = Group.objects.all()
    serializer_class = GroupSerialzier
    pagination_class = PageNum

    # 获取权限表数据
    def simple(self, reqeust):
        pers = Permission.objects.all()
        ser = PermissionSerialize(pers, many=True)  # 使用以前定义的全选序列化器
        return Response(ser.data)


class AdminView(ModelViewSet):
    queryset = User.objects.all()
    serializer_class = UserSerialzier
    pagination_class = PageNum

    def simple(self, reqeust):
        pers = Group.objects.all()
        ser = GroupSerialzier(pers, many=True)
        return Response(ser.data)
